package com.formina.stars;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;



public class Preferences extends Activity {
	
	SeekBar sbRed,sbBlue,sbGreen;
	RadioGroup rg;
	RadioButton rRandom, rCustom;
	Button bDone;
	View vColorShower;
	
	public static final String RED = "RED";
	public static final String GREEN = "GREEN";
	public static final String BLUE = "BLUE";
	public static final String ISRANDOM = "ISRANDOM";
	
	private int r,b,g;
	private boolean random = true;
	public static final String PREFS ="MINASSTARSPREFS";
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.prefs);

        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);  
        //getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);  
        
        
        Log.d("PREFERENCES", "SET VIEW");
        rg = (RadioGroup)findViewById(R.id.rgColors);
        rRandom = (RadioButton)findViewById(R.id.rRandom);
        rCustom = (RadioButton)findViewById(R.id.rCustom);

        Log.d("PREFERENCES", "SET RADIO BUTTONS");

        
        sbRed = (SeekBar)findViewById(R.id.sbRed);
        sbBlue = (SeekBar)findViewById(R.id.sbBlue);
        sbGreen = (SeekBar)findViewById(R.id.sbGreen);
        sbRed.setOnSeekBarChangeListener(sliderChange);
        sbBlue.setOnSeekBarChangeListener(sliderChange);
        sbGreen.setOnSeekBarChangeListener(sliderChange);
        bDone = (Button)findViewById(R.id.btnDone);
        bDone.setOnClickListener(bDone_Click);
        rg.setOnCheckedChangeListener(checkChanged);
        vColorShower = (View)findViewById(R.id.vColorShower);
        
        Log.d("PREFERENCES", "SET SEEKBAR AND BUTTONS");

        
        sbRed.setMax(255);
        sbBlue.setMax(255);
        sbGreen.setMax(255);
        
    	SharedPreferences settings = getSharedPreferences(PREFS,0);
        random = settings.getBoolean(ISRANDOM, true);
        sbRed.setProgress(settings.getInt(RED, 1));
        sbBlue.setProgress(settings.getInt(BLUE, 0));
        sbGreen.setProgress(settings.getInt(GREEN, 0));
        
        if(random){
        	rRandom.setChecked(random);
        }else{
        	rCustom.setChecked(!random);
        }
        EnableSliders(random);
		vColorShower.setBackgroundColor(Color.argb(255, sbRed.getProgress(), sbBlue.getProgress(), sbGreen.getProgress()));
		

	}

	
	private OnCheckedChangeListener checkChanged = new OnCheckedChangeListener(){

	
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			boolean isRandom = false;
			if(rg.getCheckedRadioButtonId()== R.id.rRandom){
				isRandom = true;
			}
			EnableSliders(isRandom);
		}
		
	};
	
	private void EnableSliders(boolean isRandom){
		if(isRandom){
			sbRed.setEnabled(false);
			sbBlue.setEnabled(false);
			sbGreen.setEnabled(false);
		}else{
			sbRed.setEnabled(true);
			sbBlue.setEnabled(true);
			sbGreen.setEnabled(true);
		}
		random = isRandom;
	}
	
	private OnClickListener bDone_Click = new OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent resultIntent = new Intent();
			resultIntent.putExtra(RED, sbRed.getProgress());
			resultIntent.putExtra(BLUE, sbBlue.getProgress());
			resultIntent.putExtra(GREEN, sbGreen.getProgress());
			random = false;
			if(rg.getCheckedRadioButtonId()== R.id.rRandom){
				random = true;
			}
			resultIntent.putExtra(ISRANDOM, random);
			setResult(Activity.RESULT_OK, resultIntent);
			SharedPreferences settings= getSharedPreferences(PREFS,0);
			SharedPreferences.Editor editor = settings.edit();
			editor.putBoolean(ISRANDOM, random);
			editor.putInt(RED, sbRed.getProgress());
			editor.putInt(BLUE, sbBlue.getProgress());
			editor.putInt(GREEN, sbGreen.getProgress());		
			editor.commit();
			finish();
		}
		
	};
	
	 private SeekBar.OnSeekBarChangeListener sliderChange = new OnSeekBarChangeListener(){

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			vColorShower.setBackgroundColor(Color.argb(255, sbRed.getProgress(), sbBlue.getProgress(), sbGreen.getProgress()));
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
		}
     
     };
}
